
package com.platform.modules.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platform.common.utils.Query;
import com.platform.modules.mall.dao.MallGoodsToActivityDao;
import com.platform.modules.mall.entity.MallGoodsToActivityEntity;
import com.platform.modules.mall.service.MallGoodsToActivityService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * 活动商品对应表Service实现类
 *
 * @author 
 * @date 2021-08-11 16:21:47
 */
@Service("mallGoodsToActivityService")
public class MallGoodsToActivityServiceImpl extends ServiceImpl<MallGoodsToActivityDao, MallGoodsToActivityEntity> implements MallGoodsToActivityService {

    @Override
    public List<MallGoodsToActivityEntity> queryAll(Map<String, Object> params) {
        return baseMapper.queryAll(params);
    }

    @Override
    public Page queryPage(Map<String, Object> params) {
        //排序
        params.put("sidx", "T.SORT");
        params.put("asc", false);
        Page<MallGoodsToActivityEntity> page = new Query<MallGoodsToActivityEntity>(params).getPage();
        return page.setRecords(baseMapper.selectMallGoodsToActivityPage(page, params));
    }

    @Override
    public boolean add(MallGoodsToActivityEntity mallGoodsToActivity) {
        return this.save(mallGoodsToActivity);
    }

    @Override
    public boolean update(MallGoodsToActivityEntity mallGoodsToActivity) {
        return this.updateById(mallGoodsToActivity);
    }

    @Override
    public boolean delete(String id) {
        return this.removeById(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(String[] ids) {
        return this.removeByIds(Arrays.asList(ids));
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deductionNumber(String id, Integer number) {
        return  this.lambdaUpdate().setSql("expend_count=expend_count-"+number)
            .eq(MallGoodsToActivityEntity::getId, id)
            .ge(MallGoodsToActivityEntity::getExpendCount, number)
            .update();
    }

    @Override
    public MallGoodsToActivityEntity getByActivityIdAndGoodsId(String activityId, String goodsId) {
        return this.lambdaQuery().eq(MallGoodsToActivityEntity::getActivityId, activityId).eq( MallGoodsToActivityEntity::getGoodsId, goodsId).one();
    }


}
